﻿using MeterReader.PubSubEvents;
using MeterReader.WPF.Models;
using MeterReader.WPF.Services;
using System.Collections.ObjectModel;

namespace MeterReader.WPF.ViewModels
{
    public class MetersCurveViewModel : BindableBase
    {
        private readonly IReadData _dataService;
        private readonly IEventAggregator _eventAggregator;

        public MetersCurveViewModel(IReadData dataService, IEventAggregator eventAggregator)
        {
			_dataService = dataService;
			_eventAggregator = eventAggregator;
			MeterDataCollections = _dataService.GetMeterDataCollections();
			GoBackDataMonitorCommand = new DelegateCommand<string>(GoBackDataMonitor);
			OpenPopupCommand = new DelegateCommand<MeterDataCollection>(OpenPopup);
		}
		#region properties

		public ObservableCollection<MeterDataCollection> MeterDataCollections { get; set; } = new();

		private bool _isPopupOpen = false;

		public bool IsPopupOpen
		{
			get { return _isPopupOpen; }
			set { SetProperty(ref _isPopupOpen, value); }
		}

		private MeterDataCollection _popupMeterDatas;

		public MeterDataCollection PopupMeterDatas
		{
			get { return _popupMeterDatas; }
			set { SetProperty(ref _popupMeterDatas, value); }
		}

		public DelegateCommand<string> GoBackDataMonitorCommand { get; set; }
		public DelegateCommand<MeterDataCollection> OpenPopupCommand { get; set; }

		#endregion properties
		private void GoBackDataMonitor(string obj) => _eventAggregator.GetEvent<NavigateEvent>().Publish(obj);

		private void OpenPopup(MeterDataCollection collection)
		{
			if (collection != null)
			{
				PopupMeterDatas = collection;
				IsPopupOpen = true;
			}
		}
	}
}